<template>
  <div class="highlight">
    <!-- <navBar></navBar> -->
    <!-- tab栏 -->
    <div class="tab">
      <van-tabs
        v-model="nameActive"
        swipeable
        line-height="0px"
        @click="onAttention"
      >
        <van-tab :name="0">
          <template #title>
            <div class="title" :class="{ active: 0 === nameActive }">集锦</div>
          </template>
        </van-tab>
        <van-tab :name="1">
          <template #title>
            <div class="title" :class="{ active: currentIndex === 1 }">
              回放
            </div>
          </template>
        </van-tab>
      </van-tabs>
    </div>
    <van-pull-refresh
      v-model="dropLoading"
      success-text="刷新成功"
      @refresh="dropRefresh"
    >
      <van-list
        v-model="pullLoading"
        :finished="finished"
        finished-text="没有更多了"
        @load="pullLoad"
      >
        <!-- 集锦部分 -->
        <div v-if="this.nameActive === 1">
          <div class="list">
            <div
              class="video-item"
              v-for="(item, index) in highlightLists"
              :key="index"
            >
              <div class="v-box">
                <div class="title">{{ item.title }}</div>
                <img class="img" :src="item.image" alt="" />
                <i class="iconfont">&#xe7bf;</i>
                <div class="duration">{{ item.videoTimeDesc }}</div>
              </div>
              <div class="data">
                <div class="view">
                  <span
                    ><i class="iconfont">&#xe60e;</i
                    >{{ item.readCount }}万次观看</span
                  >
                </div>
                <div class="collect">
                  <span
                    ><i class="iconfont">&#xe612;</i
                    >{{ item.collectionCount }}收藏</span
                  >
                </div>
                <div class="like">
                  <span
                    ><i class="iconfont">&#xe60f;</i
                    >{{ item.praiseCount }}点赞</span
                  >
                </div>
              </div>
            </div>
          </div>
        </div>

        <!-- 回放部分 -->
        <div v-else>
          <div class="list">
            <div
              class="video-item"
              v-for="(item, index) in highlightList"
              :key="index"
            >
              <div class="v-box">
                <div class="title">{{ item.title }}</div>
                <img class="img" :src="item.image" alt="" />
                <i class="iconfont">&#xe7bf;</i>
                <div class="duration">{{ item.videoTimeDesc }}</div>
              </div>
              <div class="data">
                <div class="view">
                  <span
                    ><i class="iconfont">&#xe60e;</i
                    >{{ item.readCount }}万次观看</span
                  >
                </div>
                <div class="collect">
                  <span
                    ><i class="iconfont">&#xe612;</i
                    >{{ item.collectionCount }}收藏</span
                  >
                </div>
                <div class="like">
                  <span
                    ><i class="iconfont">&#xe60f;</i
                    >{{ item.praiseCount }}点赞</span
                  >
                </div>
              </div>
            </div>
          </div>
        </div>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
import { videoList } from '@/api/find.js'
// import navBar from '@/components/navBar'
export default {
  name: 'highlight',
  // components: {
  //   navBar
  // },
  data () {
    return {
      nameActive: 0, // 默认选中
      currentIndex: 0,
      highlightList: [],
      highlightLists: [],
      dropLoading: false,
      pullLoading: false,
      finished: false,
      current: 1,
      pageSize: 10
    }
  },
  async created () {
    const res = await videoList({
      pageNum: (this.current - 1) * this.pageSize,
      pageSize: this.pageSize,
      query: { contentVideoType: this.currentIndex.toString() }
    })
    this.highlightList = res.data.data.records
  },
  methods: {
    // 封装 点击事件和下拉刷新
    async tabClick () {
      if (this.nameActive === 0) {
        const res = await videoList({
          pageNum: (this.current - 1) * this.pageSize,
          pageSize: this.pageSize,
          query: { contentVideoType: '1' }
        })
        this.highlightList = res.data.data.records
      } else if (this.currentIndex === 1) {
        const res = await videoList({
          pageNum: (this.current - 1) * this.pageSize,
          pageSize: this.pageSize,
          query: { contentVideoType: '2' }
        })
        this.highlightLists = res.data.data.records
      }
    },
    // 封装 上拉加载
    async pull () {
      if (this.nameActive === 0) {
        const res = await videoList({
          pageNum: (this.current - 1) * this.pageSize,
          pageSize: this.pageSize,
          query: { contentVideoType: '1' }
        })
        this.highlightList.push(...res.data.data.records)
        this.pullLoading = false
        this.current++
        if (this.highlightList.length >= res.data.data.total) {
          this.finished = true
        }
      } else if (this.currentIndex === 1) {
        const res = await videoList({
          pageNum: (this.current - 1) * this.pageSize,
          pageSize: this.pageSize,
          query: { contentVideoType: '2' }
        })
        this.highlightLists.push(...res.data.data.records)
        this.pullLoading = false
        this.current++
        if (this.highlightLists.length >= res.data.data.total) {
          this.finished = true
        }
      }
    },
    // tab栏点击事件
    async onAttention () {
      this.currentIndex = this.nameActive
      if (this.highlightList.length === 0 || this.highlightLists.length === 0) {
        this.tabClick()
      }
    },
    // 上拉加载
    async pullLoad () {
      this.pull()
    },
    // 下拉刷新
    async dropRefresh () {
      this.tabClick()
      this.dropLoading = false
    }
  }
}
</script>

<style lang="less">
.highlight {
  width: 100%;
  .tab {
    width: 100%;
    height: 50px;
    border-bottom: 1px solid #ccc;
  }
  .list {
    background-color: #fff;
    margin-bottom: 10px;
    .video-item {
      width: 100%;
      display: block;
      height: 260px;
      object-fit: cover;
      .v-box {
        overflow: hidden;
        position: relative;
        .title {
          width: 100%;
          height: 100%;
          padding: 7px 16px 0;
          position: absolute;
          left: 0;
          z-index: 2;
          font-size: 16px;
          color: #fff;
          font-weight: 700;
          background: linear-gradient(rgba(0, 0, 0, 0.4), transparent);
        }
        .img {
          width: 100%;
          height: 210px;
        }
        .iconfont {
          position: absolute;
          top: 50%;
          left: 50%;
          transform: translate(-50%, -50%);
          color: #fff;
          font-size: 40px;
        }
        .iconfont {
          z-index: 115;
          overflow: hidden;
          cursor: pointer;
          display: block;
          border-radius: 50%;
        }
        .duration {
          position: absolute;
          bottom: 20px;
          right: 20px;
          color: #fff;
          padding: 3px 6px;
          font-size: 12px;
          border-radius: 9px;
          background-color: rgba(0, 0, 0, 0.65);
        }
      }
      .data {
        overflow: hidden;
        padding: 0 15px;
        .view {
          float: left;
          font-size: 14px;
          color: #888;
          display: flex;
          padding: 5px 0;
          line-height: 20px;
          align-items: center;
          span {
            .iconfont {
              font-size: 14px;
              margin-right: 5px;
            }
          }
        }
        .collect {
          float: left;
          font-size: 14px;
          color: #888;
          display: flex;
          padding: 5px 15px;
          line-height: 20px;
          align-items: center;
          span {
            .iconfont {
              font-size: 14px;
              margin-right: 5px;
            }
          }
        }
        .like {
          float: right;
          font-size: 14px;
          color: #888;
          display: flex;
          padding: 5px 0;
          line-height: 20px;
          align-items: center;
          span {
            .iconfont {
              font-size: 14px;
              margin-right: 5px;
            }
          }
        }
      }
    }
  }
  .active {
    width: 56px;
    height: 28px;
    line-height: 28px;
    border-radius: 15px;
    background-color: #0085ff;
    color: #fff;
    text-align: center;
  }
}
</style>
